﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb
{
    public partial class ManageUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblHeader.Text = "Manage HQ Users";
            if (!IsPostBack)
            {
                UCGrid1.SetDataSource(GrdSource());
            }
        }


        private DataTable GrdSource()
        {
            DataTable usrTable = new DataTable();
            DataRow newRow;

            usrTable.Columns.Add("Id");
            usrTable.Columns.Add("Name");
            usrTable.Columns.Add("Last Updated By");
            usrTable.Columns.Add("Last Updated Date");


            foreach (MembershipUser usr in Membership.GetAllUsers())
            {
                Guid userGuuid = (Guid)usr.ProviderUserKey;

                StructureFacade StructureFacade = new StructureFacade();
                User usrObj = StructureFacade.GetUser(userGuuid);
                MembershipUser adUser = Membership.GetUser(usrObj.LastUpBy);
                User adObj = StructureFacade.GetUser(usrObj.LastUpBy);

                newRow = usrTable.NewRow(); 
                newRow[0] = usr.UserName;
                newRow[1] = usrObj.Name;
                newRow[2] = adObj.Name;
                newRow[3] = usrObj.LastUpdateDate.ToShortDateString();
                usrTable.Rows.Add(newRow);
            }

            return usrTable;

        }
    }
}
